package lifeOfAFish.fishModels.amago;

import lifeOfAFish.Fish;
import lifeOfAFish.scenegraph.sgNode;
import lifeOfAFish.scenegraph.sgTransform;
import lifeOfAFish.scenegraph.geom.sgGeomModel;

/**
 * @author Christian Mletzko
 *
 */
public class Amago extends Fish {

	
	private int frameCounter = Integer.MIN_VALUE;
	private int finCounter = -15;
	private int slowFinCounter = -2;
	private boolean finMovementUp;
	private boolean slowFinMovementUp;
	
	private AmagoPreferences a_prefs;
	private sgNode head;
	private sgGeomModel body;	
	private sgGeomModel fin_bottom_middle_left;
	private sgGeomModel fin_bottom_middle_right;
	private sgGeomModel fin_bottom;
	private sgGeomModel fin_front_middle_left;
	private sgGeomModel fin_front_middle_right;
	private sgGeomModel fin_main;
	private sgGeomModel fin_top_front;
	private sgGeomModel fin_top_middle;	
	
	private sgTransform fin_bottom_middle_leftRotation;
	private sgTransform fin_bottom_middle_rightRotation;
	private sgTransform fin_bottomRotation;
	private sgTransform fin_front_middle_leftRotation;
	private sgTransform fin_front_middle_rightRotation;
	private sgTransform fin_mainRotation;
	private sgTransform fin_top_frontRotation;
	private sgTransform fin_top_middleRotation;
	
	private sgTransform amagoTransform;
	public sgTransform r1;
	private sgTransform t1;
	float theta = 0.0f;
	float step = 0.5f;
	public boolean move = false;
	
	
	public Amago(){
		
		a_prefs = new AmagoPreferences();
		head = new sgNode();
		body = new sgGeomModel();
		fin_bottom_middle_left = new sgGeomModel();
		fin_bottom_middle_right = new sgGeomModel();
		fin_bottom = new sgGeomModel();
		fin_front_middle_left = new sgGeomModel();
		fin_front_middle_right = new sgGeomModel();
		fin_main = new sgGeomModel();
		fin_top_front = new sgGeomModel();
		fin_top_middle = new sgGeomModel();
		
		
		
	}
	
	
	public void init() {
	
		// Load meshes
		body.setOBJFileName(a_prefs.getBODY());
		body.load();
		
		
		fin_bottom_middle_left.setOBJFileName(a_prefs.getFIN_BOTTOM_MIDDLE_LEFT());
		fin_bottom_middle_left.load();
		
		
		fin_bottom_middle_right.setOBJFileName(a_prefs.getFIN_BOTTOM_MIDDLE_RIGHT());
		fin_bottom_middle_right.load();
		
		fin_bottom.setOBJFileName(a_prefs.getFIN_BOTTOM());
		fin_bottom.load();
		
		fin_front_middle_left.setOBJFileName(a_prefs.getFIN_FRONT_MIDDLE_LEFT());
		fin_front_middle_left.load();
		
		fin_front_middle_right.setOBJFileName(a_prefs.getFIN_FRONT_MIDDLE_RIGHT());
		fin_front_middle_right.load();
		
		fin_main.setOBJFileName(a_prefs.getFIN_MAIN());
		fin_main.load();
		
		fin_top_front.setOBJFileName(a_prefs.getFIN_TOP_FRONT());
		fin_top_front.load();
		
		fin_top_middle.setOBJFileName(a_prefs.getFIN_TOP_MIDDLE());
		fin_top_middle.load();
		
		
		
		
		
		/* Add transformation nodes for fins
		 * 1. Rotation
		 * 2. Translation
		 */
		
		// Rotate body
		sgTransform bodyRotation = new sgTransform();
		bodyRotation.rotate(0.0f, 1.0f, 0.0f, 0.0f);
		// Translate body
		sgTransform bodyTranslation = new sgTransform();
		bodyTranslation.translate(0.0f, 0.0f, 0.0f);
		bodyRotation.addChild(bodyTranslation);
		
		
		sgTransform scaleB1 = new sgTransform();
		scaleB1.scale(100.0f, 100.0f, 100.0f);
		bodyTranslation.addChild(scaleB1);
		scaleB1.addChild(body);
		
		head.addChild(bodyRotation);
		
		
		
		// Rotate fin_bottom_middle_left
		fin_bottom_middle_leftRotation = new sgTransform();
		fin_bottom_middle_leftRotation.rotate(0.0f, 1.0f, 0.0f, 0.0f);
		// Translate fin_bottom_middle_left
		sgTransform fin_bottom_middle_leftTranslation = new sgTransform();
		fin_bottom_middle_leftTranslation.translate(0.0f, 0.0f, 0.0f);
		fin_bottom_middle_leftRotation.addChild(fin_bottom_middle_leftTranslation);
				
		sgTransform scaleB2 = new sgTransform();
		scaleB2.scale(100.0f, 100.0f, 100.0f);
		fin_bottom_middle_leftTranslation.addChild(scaleB2);
		scaleB2.addChild(fin_bottom_middle_left);
		
		head.addChild(fin_bottom_middle_leftRotation);
		
		
		
		// Rotate fin_bottom_middle_right
		fin_bottom_middle_rightRotation = new sgTransform();
		fin_bottom_middle_rightRotation.rotate(0.0f, 1.0f, 0.0f, 0.0f);
		// Translate body
		sgTransform fin_bottom_middle_rightTranslation = new sgTransform();
		fin_bottom_middle_rightTranslation.translate(0.0f, 0.0f, 0.0f);
		fin_bottom_middle_rightRotation.addChild(fin_bottom_middle_rightTranslation);
				
		sgTransform scaleB3 = new sgTransform();
		scaleB3.scale(100.0f, 100.0f, 100.0f);
		fin_bottom_middle_rightTranslation.addChild(scaleB3);
		scaleB3.addChild(fin_bottom_middle_right);
		
		head.addChild(fin_bottom_middle_rightRotation);
		
		
		
		// Rotate fin_bottom
		fin_bottomRotation = new sgTransform();
		fin_bottomRotation.rotate(0.0f, 1.0f, 0.0f, 0.0f);
		// Translate fin_bottom
		sgTransform fin_bottomTranslation = new sgTransform();
		fin_bottomTranslation.translate(0.0f, 0.0f, 0.0f);
		fin_bottomRotation.addChild(fin_bottomTranslation);
		
		sgTransform scaleB4 = new sgTransform();
		scaleB4.scale(100.0f, 100.0f, 100.0f);
		fin_bottomTranslation.addChild(scaleB4);
		scaleB4.addChild(fin_bottom);
		
		head.addChild(fin_bottomRotation);
		
		
		// Rotate fin_front_middle_left
		fin_front_middle_leftRotation = new sgTransform();
		fin_front_middle_leftRotation.rotate(0.0f, 1.0f, 0.0f, 0.0f);
		// Translate fin_front_middle_left
		sgTransform fin_front_middle_leftTranslation = new sgTransform();
		fin_front_middle_leftTranslation.translate(0.0f, 0.0f, 0.0f);
		fin_front_middle_leftRotation.addChild(fin_front_middle_leftTranslation);
		
		sgTransform scaleB5 = new sgTransform();
		scaleB5.scale(100.0f, 100.0f, 100.0f);
		fin_front_middle_leftTranslation.addChild(scaleB5);
		scaleB5.addChild(fin_front_middle_left);
		
		head.addChild(fin_front_middle_leftRotation);
		
		
		// Rotate fin_front_middle_right
		fin_front_middle_rightRotation = new sgTransform();
		fin_front_middle_rightRotation.rotate(0.0f, 1.0f, 0.0f, 0.0f);
		// Translate fin_front_middle_right
		sgTransform fin_front_middle_rightTranslation = new sgTransform();
		fin_front_middle_rightTranslation.translate(0.0f, 0.0f, 0.0f);
		fin_front_middle_rightRotation.addChild(fin_front_middle_rightTranslation);
		
		sgTransform scaleB6 = new sgTransform();
		scaleB6.scale(100.0f, 100.0f, 100.0f);
		fin_front_middle_rightTranslation.addChild(scaleB6);
		scaleB6.addChild(fin_front_middle_right);
		
		head.addChild(fin_front_middle_rightRotation);
		
		
		// Rotate fin_main
		fin_mainRotation = new sgTransform();
		fin_mainRotation.rotate(0.0f, 1.0f, 0.0f, 0.0f);
		// Translate fin_main
		sgTransform fin_mainTranslation = new sgTransform();
		fin_mainTranslation.translate(0.0f, 0.0f, 0.0f);
		fin_mainRotation.addChild(fin_mainTranslation);
		
		sgTransform scaleB7 = new sgTransform();
		scaleB7.scale(100.0f, 100.0f, 100.0f);
		fin_mainTranslation.addChild(scaleB7);
		scaleB7.addChild(fin_main);
		
		head.addChild(fin_mainRotation);
		
		
		// Rotate fin_top_front
		fin_top_frontRotation = new sgTransform();
		fin_top_frontRotation.rotate(0.0f, 1.0f, 0.0f, 0.0f);
		// Translate fin_top_front
		sgTransform fin_top_frontTranslation = new sgTransform();
		fin_top_frontTranslation.translate(0.0f, 0.0f, 0.0f);
		fin_top_frontRotation.addChild(fin_top_frontTranslation);
		
		sgTransform scaleB8 = new sgTransform();
		scaleB8.scale(100.0f, 100.0f, 100.0f);
		fin_top_frontTranslation.addChild(scaleB8);
		scaleB8.addChild(fin_top_front);
		
		head.addChild(fin_top_frontRotation);
		
		
		// Rotate fin_top_middle
		fin_top_middleRotation = new sgTransform();
		fin_top_middleRotation.rotate(0.0f, 1.0f, 0.0f, 0.0f);
		// Translate fin_top_middle
		sgTransform fin_top_middleTranslation = new sgTransform();
		fin_top_middleTranslation.translate(0.0f, 0.0f, 0.0f);
		fin_top_middleRotation.addChild(fin_top_middleTranslation);
		
		sgTransform scaleB9 = new sgTransform();
		scaleB9.scale(100.0f, 100.0f, 100.0f);
		fin_top_middleTranslation.addChild(scaleB9);
		scaleB9.addChild(fin_top_middle);
		
		head.addChild(fin_top_middleRotation);
		
		
		
		// Add head node to local root
		r1 = new sgTransform();
		r1.rotate(0.0f, 0.0f, 0.0f, 0.0f);
		t1 = new sgTransform();
		t1.translate(0.2f, 0.0f, -0.1f);
		
		amagoTransform = new sgTransform();
		amagoTransform.scale(0.30f, 0.3f, 0.3f);
		m_node.addChild(amagoTransform);
		amagoTransform.addChild(r1);
		r1.addChild(t1);
		t1.addChild(head);
		
		finMovementUp = true;
		slowFinMovementUp = true;
	}
	
	
	
	/**
	 * 
	 */
	public void update() {
		
		incFrameCounter();
		
		// Movement of the fins
		if (frameCounter%3==0){
			
			if (finMovementUp){
				incFinCounter();
			}
			else if(!finMovementUp){
				decFinCounter();
			}
		}
		
		// Movement of the slow fins
		if (frameCounter%8==0){
			
			if (slowFinMovementUp){
				incSlowFinCounter();
			}
			else if(!slowFinMovementUp){
				decSlowFinCounter();
			}
		}
		
		

		
		fin_bottom_middle_leftRotation.rotate(finCounter, 1.0f, 0.0f, 0.0f);
		fin_bottom_middle_rightRotation.rotate(-finCounter, 1.0f, 0.0f, 0.0f);
		fin_bottomRotation.rotate(finCounter, 1.0f, 0.0f, 0.0f);
		fin_front_middle_leftRotation.rotate(finCounter, 1.0f, 0.0f, 0.0f);
		fin_front_middle_rightRotation.rotate(-finCounter, 1.0f, 0.0f, 0.0f);
		fin_mainRotation.rotate(slowFinCounter, 0.0f, 1.0f, 0.0f);
		fin_top_frontRotation.rotate(slowFinCounter, 1.0f, 0.0f, 0.0f);
		fin_top_middleRotation.rotate(-finCounter, 1.0f, 0.0f, 0.0f);

		if(move){
		r1.rotate(-theta/3, 0.2f, 1.0f, 0.0f);
		theta += step;
		}
		
	}
	
	
	/**
	 * This method increases the frame counter
	 * 
	 * @see getFrameCounter()
	 */
	private void incFrameCounter(){
		if (frameCounter < Integer.MAX_VALUE){
			frameCounter++;
		}
		else
			frameCounter = Integer.MIN_VALUE;
	}
	
	/**
	 * This method increases the fin counter
	 * 
	 * @see getFrameCounter()
	 */
	private void incFinCounter(){
		
			finCounter++;
			if (finCounter==15.0){
				finMovementUp = false;
			}
	}
		
	/**
	 * This method increases the fin counter
	 * 
	 * @see getFrameCounter()
	 */
	private void decFinCounter(){
		
			finCounter--;
			if (finCounter==-15.0){
				finMovementUp = true;
			}
	
	}
	/**
	 * This method increases the fin counter
	 * 
	 * @see getFrameCounter()
	 */
	private void incSlowFinCounter(){
		
			slowFinCounter++;
			if (slowFinCounter==2.0f){
				slowFinMovementUp = false;
			}
	}
		
	/**
	 * This method increases the fin counter
	 * 
	 * @see getFrameCounter()
	 */
	private void decSlowFinCounter(){
		
			slowFinCounter--;
			if (slowFinCounter==-2.0f){
				slowFinMovementUp = true;
			}
	
	}
}
